package com.yanqu.road.dao.impl.activity.peakstronghold;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.peakstronghold.*;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossPeakStrongholdUnionDataDaoImpl extends TempDataObjectDao<CrossPeakStrongholdUnionData> {
    public static String getCrossPeakStrongholdUnionDataSql = "select * from t_c_activity_peak_stronghold_union where activityId=? and unionGroup=?";

    public Map<String, CrossPeakStrongholdUnionData> getCrossPeakStrongholdUnionData(int activityId, int unionGroup) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, CrossPeakStrongholdUnionData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossPeakStrongholdUnionDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, unionGroup);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossPeakStrongholdUnionData data = parse(rs);
                dataMap.put(data.getUnionId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateCrossPeakStrongholdUnionDataSql = "update `t_c_activity_peak_stronghold_union` set unionBaseInfo=?,groupNum=?,rank=?,lastRound=?,isCreateDefend=?,score=?,totalDonateExp=?,totalDonateSilver=?,memberInfoMap=?,defendPatrons=?,lastFightTime=?,randSeed=?,totalDamage=?,scoreRank=?,silverRank=?,ability=?,memberDamageMap=?,unionDamageMap=?,totalGroupScore=? where activityId=? and unionId=? and unionGroup=?";
    @Override
    protected boolean updateData(CrossPeakStrongholdUnionData crossPeakStrongholdUnionData) {
        crossPeakStrongholdUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossPeakStrongholdUnionData.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, crossPeakStrongholdUnionData.getGroup()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPeakStrongholdUnionData.getRank()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPeakStrongholdUnionData.getLastRound()));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossPeakStrongholdUnionData.isCreateDefend()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossPeakStrongholdUnionData.getScore().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPeakStrongholdUnionData.getTotalDonateExp()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossPeakStrongholdUnionData.getTotalDonateSilver().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossPeakStrongholdUnionData.getMemberInfoMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossPeakStrongholdUnionData.getDefendPatrons())));
        para.put(i++, new DbParameter(Types.BIGINT, crossPeakStrongholdUnionData.getLastFightTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPeakStrongholdUnionData.getRandSeed()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossPeakStrongholdUnionData.getTotalDamage().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPeakStrongholdUnionData.getScoreRank()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPeakStrongholdUnionData.getSilverRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossPeakStrongholdUnionData.getAbility().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossPeakStrongholdUnionData.getMemberDamageMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossPeakStrongholdUnionData.getUnionDamageMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, crossPeakStrongholdUnionData.getTotalGroupScore().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPeakStrongholdUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossPeakStrongholdUnionData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPeakStrongholdUnionData.getUnionGroup()));
        if (getDb().executeNoneQuery(updateCrossPeakStrongholdUnionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossPeakStrongholdUnionDataSql = "insert into `t_c_activity_peak_stronghold_union`(activityId,unionGroup,unionId,unionBaseInfo,groupNum,rank,lastRound,isCreateDefend,score,totalDonateExp,totalDonateSilver,memberInfoMap,defendPatrons,lastFightTime,randSeed,totalDamage,scoreRank,silverRank,ability,memberDamageMap,unionDamageMap,totalGroupScore) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossPeakStrongholdUnionData crossPeakStrongholdUnionData) {
        crossPeakStrongholdUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossPeakStrongholdUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPeakStrongholdUnionData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossPeakStrongholdUnionData.getUnionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossPeakStrongholdUnionData.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, crossPeakStrongholdUnionData.getGroup()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPeakStrongholdUnionData.getRank()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPeakStrongholdUnionData.getLastRound()));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossPeakStrongholdUnionData.isCreateDefend()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossPeakStrongholdUnionData.getScore().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPeakStrongholdUnionData.getTotalDonateExp()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossPeakStrongholdUnionData.getTotalDonateSilver().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossPeakStrongholdUnionData.getMemberInfoMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossPeakStrongholdUnionData.getDefendPatrons())));
        para.put(i++, new DbParameter(Types.BIGINT, crossPeakStrongholdUnionData.getLastFightTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPeakStrongholdUnionData.getRandSeed()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossPeakStrongholdUnionData.getTotalDamage().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPeakStrongholdUnionData.getScoreRank()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPeakStrongholdUnionData.getSilverRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossPeakStrongholdUnionData.getAbility().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossPeakStrongholdUnionData.getMemberDamageMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossPeakStrongholdUnionData.getUnionDamageMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, crossPeakStrongholdUnionData.getTotalGroupScore().toString()));
        if (getDb().executeNoneQuery(insertCrossPeakStrongholdUnionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossPeakStrongholdUnionData parseData(ResultSet rs) throws SQLException {
        CrossPeakStrongholdUnionData data = new CrossPeakStrongholdUnionData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUnionGroup(rs.getInt("unionGroup"));
        data.setUnionId(rs.getString("unionId"));
        data.setUnionBaseInfo(JSON.parseObject(rs.getString("unionBaseInfo"), new TypeReference<UnionBaseInfo>() {
        }));
        data.setGroup(rs.getInt("groupNum"));
        data.setRank(rs.getInt("rank"));
        data.setLastRound(rs.getInt("lastRound"));
        data.setCreateDefend(rs.getBoolean("isCreateDefend"));
        data.setScore(new BigInteger(rs.getString("score")));
        data.setTotalDonateExp(rs.getInt("totalDonateExp"));
        data.setTotalDonateSilver(new BigInteger(rs.getString("totalDonateSilver")));
        data.setMemberInfoMap(JSON.parseObject(rs.getString("memberInfoMap"), new TypeReference<Map<Long, PeakStrongholdUnionMemberInfo>>() {
        }));
        data.setDefendPatrons(JSON.parseObject(rs.getString("defendPatrons"), new TypeReference<Map<Long, List<PeakStrongholdDefendPatrons>>>() {
        }));
        data.setLastFightTime(rs.getLong("lastFightTime"));
        data.setRandSeed(rs.getInt("randSeed"));
        data.setTotalDamage(new BigInteger(rs.getString("totalDamage")));
        data.setScoreRank(rs.getInt("scoreRank"));
        data.setSilverRank(rs.getInt("silverRank"));
        data.setAbility(new BigInteger(rs.getString("ability")));
        String str = rs.getString("memberDamageMap");
        if(!StringUtils.isNullOrEmpty(str) && !"null".equalsIgnoreCase(str)){
            data.setMemberDamageMap(JSON.parseObject(str, new TypeReference<ConcurrentHashMap<Long, List<PeakStrongholdMemberDamage>>>(){}));
        }
        str = rs.getString("unionDamageMap");
        if(!StringUtils.isNullOrEmpty(str) && !"null".equalsIgnoreCase(str)){
            data.setUnionDamageMap(JSON.parseObject(str,new TypeReference<ConcurrentHashMap<Integer, PeakStrongholdUnionDamage>>(){}));
        }
        str = rs.getString("totalGroupScore");
        if(!StringUtils.isNullOrEmpty(str) && !"null".equalsIgnoreCase(str)){
            data.setTotalGroupScore(new BigInteger(str));
        }

        return data;
    }
}
